package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.constants.RedisConstants;
import com.ytjj.common.service.RedisService;
import com.ytjj.qmyx.admin.mapper.ReportsMapper;
import com.ytjj.qmyx.admin.model.YxReports;
import com.ytjj.qmyx.admin.model.dto.YxReportsDto;
import com.ytjj.qmyx.admin.model.vo.ChattingRecordsVo;
import com.ytjj.qmyx.admin.model.vo.YxReportsVo;
import com.ytjj.qmyx.admin.service.RePortsService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
public class RePortsServiceImpl implements RePortsService {

    private RedisService redisService;

    private ReportsMapper reportsMapper;

    @Override
    public CommonPage<YxReportsVo> selectReportsPage(YxReportsDto yxReportsDto) {
        PageHelper.startPage(yxReportsDto.getPageNum(),yxReportsDto.getPageSize());
        return CommonPage.restPage(reportsMapper.selectReportsPage(yxReportsDto));
    }

    @Override
    public Boolean insertReprotMessage(YxReports yxReports) {
        yxReports.setCreateTime(new Date());
        // 修改举报人与被举报人会话状态
        reportsMapper.updateSessionState(yxReports.getUroraSessionInfoId());
        // 插入举报信息
        Boolean message = reportsMapper.insertReprotMessage(yxReports);
        return message;
    }

    @Override
    public int updateStatusNoChat(Integer userId,Integer adminId,Integer id,Integer date) {
        Integer noChatNumber = date * 3600 * 24;
//        Integer noChatNumber = date * 30;
        // redis存入禁止聊天状态
        doHandelNoChat(userId, noChatNumber);

        YxReports yxReports = reportsMapper.selectById(id);
        if (null != yxReports) {
            yxReports.setStatus(2);
            yxReports.setAdminId(adminId);
            // 修改被举报人的状态为禁聊
            return reportsMapper.updateStatusNoChat(yxReports);
        }
        return 0;
    }

    @Override
    public List<ChattingRecordsVo> findReportSession(Integer sessionId) {
        return reportsMapper.findReportSession(sessionId);
    }

    @Override
    public Boolean updateStatusDoHandelr(Integer adminId,Integer id) {
        YxReports yxReports = reportsMapper.selectById(id);
        if (null != yxReports) {
            yxReports.setAdminId(adminId);
            yxReports.setStatus(3);
            return reportsMapper.updateStatusDoHandelr(yxReports);
        }
        return false;
    }

    private void doHandelNoChat(Integer userId,Integer date) {
        redisService.set(RedisConstants.NO_CHAT_KEY + userId ,"noChat");
        redisService.expire(RedisConstants.NO_CHAT_KEY + userId , date);
        System.out.println(redisService.get(RedisConstants.NO_CHAT_KEY + userId));
    }


}
